package com.zzty.txj.controller;


import com.zzty.txj.entity.DoDaily;
import com.zzty.txj.entity.Sport;
import com.zzty.txj.service.DpostService;
import com.zzty.txj.service.SportService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sport")
public class SportController {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SportController.class);
    @Autowired
    SportService sportService;
@Autowired
    DpostService dpostService;


//    @RequestMapping(value = "/insertType",method =RequestMethod.POST)
//    public Integer getType(Sport sport){
//        Integer count=sportService.typeCount(sport);
//        System.out.println("count"+count);
//        return count;
//    }

    @RequestMapping(value = "/checkType")
    public String checkType(Sport sport){
        logger.info("SportController"+sport);
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));

        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        String date2=String.valueOf(df2.format(date));
        sport.setDate(date1);
        String result="";
        List<Sport>sports=sportService.getUser(sport);

        for(int i=0;i<sports.size();i++){
            if(sport.getUserId().equals(sports.get(i).getUserId())){
                Integer count=sportService.getType(sport);
                result=String.valueOf(count);

            }
            else{

                return result;
            }
        }

        return result;
    }


    @RequestMapping(value = "/startTime",method=RequestMethod.POST)
    public Integer addStartTime(Sport sport){
        Integer count=0;
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));

        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        String date2=String.valueOf(df2.format(date));
        sport.setDate(date1);
        sport.setStartTime(date2);
        sport.setUserId(sport.getUserId());

        if(sportService.getSportType(sport).getStartTime()!=""||sportService.getSportType(sport).getStartTime()!=null){
            count=-1;
        }
        if(sportService.getSportType(sport).getStartTime()==""||sportService.getSportType(sport).getStartTime()==null
                &&sportService.getSportType(sport).getType()!=null
                ){
            count=sportService.getCount(sport);

        }
            return count;
    }

    @RequestMapping(value = "/middleTime")
    public Integer addMiddleTime(Sport sport){
        Integer count=0;
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));

        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        String date2=String.valueOf(df2.format(date));
        sport.setDate(date1);
        sport.setMiddleTime(date2);
        sport.setUserId(sport.getUserId());
        Sport sport1=sportService.getSportType(sport);
        if(sportService.getSportType(sport).getMiddleTime()!=""||sportService.getSportType(sport).getMiddleTime()!=null){
            count=-1;
        }

        if(sport1.getStartTime()!=null &&sportService.getSportType(sport).getMiddleTime()==null){
                count=sportService.getMcount(sport);
            }

        return count;
    }

    @RequestMapping(value = "/stopTime")
    public Integer addStopTime(Sport sport){
        Integer count=0;
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));

        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        String date2=String.valueOf(df2.format(date));

        sport.setUserId(sport.getUserId());
        sport.setDate(date1);
        sport.setFinishTime(date2);
        Sport sport1=sportService.getSportType(sport);

        if(sportService.getSportType(sport).getFinishTime()!=null){
            count=-1;
        }
        if(sport1.getMiddleTime()!=null&& sportService.getSportType(sport).getFinishTime()==null){
                String s1=sport1.getStartTime();
                String s2=sport.getFinishTime();
                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
                Date date11= null;
                try {
                    date11 = df.parse(s1);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Date now = null;
                try {
                    now = df.parse(s2);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long l=now.getTime()-date11.getTime();
                long day=l/(24*60*60*1000);
                long hour=(l/(60*60*1000)-day*24);
                long min=((l/(60*1000))-day*24*60-hour*60);
                long s=(l/1000-day*24*60*60-hour*60*60-min*60);
                String a=String.valueOf(hour);
                String b=String.valueOf(min);
                String c=String.valueOf(s);
                String d=a+":"+b+":"+c;

                sport.setSpendTime(d);
                Integer count1=sportService.getScount(sport);
                count=count1;

            DoDaily doDaily=new DoDaily();
            doDaily.setDate(date1);
            doDaily.setUserId(sport.getUserId());
            doDaily.setDailyId(8);
                Integer count2=dpostService.getCount(doDaily);
            }
        return count;
    }

    @RequestMapping(value = "/checkSport")
    public String checkSport(Sport sport){
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        String date2=String.valueOf(df2.format(date));
        sport.setDate(date1);
        String result="";
        List<Sport>sports=sportService.getUser(sport);
        if(sports.isEmpty()){
            Integer count=sportService.count(sport);
            result=String.valueOf(count);
        }

        for(int i=0;i<sports.size();i++){

            if(sport.getUserId().equals(sports.get(i).getUserId())&&sports.get(i).getType()!=null){
                String result1=String.valueOf(sports.get(i).getType());
                if("0".equals(result1)){
                    result="shinei";
                }
                else if("1".equals(result1)){
                    result="run";
                }

            }
            else{

                result="success";
            }
        }
        return result;
    }

    @RequestMapping(value = "/updateImg1")
    public String updateImg1(Sport sport){
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));
        sport.setDate(date1);
        String result="";
        List<Sport>sports=sportService.getUser(sport);
        if(sports.isEmpty()){
            result="null";
        }
        for(int i=0;i<sports.size();i++){

            if(sport.getUserId().equals(sports.get(i).getUserId())&&sports.get(i).getType()!=null){
                String result1=String.valueOf(sports.get(i).getType());
                if("0".equals(result1)){
                   result = String.valueOf(sportService.updateImg1(sport));
                }
               else{
                    result = "false";
                }
            }

        }
        DoDaily doDaily=new DoDaily();
        doDaily.setDate(date1);
        doDaily.setUserId(sport.getUserId());
        doDaily.setDailyId(8);
        Integer count2=dpostService.getCount(doDaily);
        return result;
    }

    @RequestMapping(value = "/updateImg2")
    public String updateImg2(Sport sport){
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));
        sport.setDate(date1);
        String result="";
        List<Sport>sports=sportService.getUser(sport);
        if(sports.isEmpty()){
            result="null";
        }
        System.out.println("sport========"+sport.toString());
        for(int i=0;i<sports.size();i++){

            if(sport.getUserId().equals(sports.get(i).getUserId())&&sports.get(i).getType()!=null){
                String result1=String.valueOf(sports.get(i).getType());
                if("0".equals(result1)){
                    result = String.valueOf(sportService.updateImg2(sport));
                }
                else{
                    result = "false";
                }

            }

        }
        DoDaily doDaily=new DoDaily();
        doDaily.setDate(date1);
        doDaily.setUserId(sport.getUserId());
        doDaily.setDailyId(8);
        Integer count2=dpostService.getCount(doDaily);
        return result;
    }
}
